﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MultiplayerDice.Implementation.Properties;

namespace MultiplayerDice.Implementation.Infrastructure
{
    public static class SqlReader
    {
        public static IEnumerable<IDataRecord> Read(string sql, params object[] parameters)
        {
            using (var c = new SqlConnection(Settings.Default.MultiplayerDiceConnectionString))
            {
                using (var co = new SqlCommand(sql, c))
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        if (parameters[i] != null)
                            co.Parameters.AddWithValue("@" + i, parameters[i]);
                    }
                    c.Open();
                    var reader = co.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                        yield return reader;
                    if (c.State != ConnectionState.Closed)
                        c.Close();
                }
            }
        }
    }
}
